﻿@page "/fetchdata"

<PageTitle>Weather forecast</PageTitle>

@inject WeatherForecastService ForecastService

<h1>Weather forecast</h1>

<p>This component demonstrates fetching data from a service.</p>

@if (forecasts == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <MDataTable Headers="_headers" Items="forecasts" ItemsPerPage="5" Class="elevation-1">
        <ItemColContent>
            @if (context.Header.Value == nameof(WeatherForecast.Date))
            {
                @context.Item.Date.ToShortDateString()
            }
            else
            {
                @context.Value
            }
            </ItemColContent>
    </MDataTable>
}

@code {
    private List<DataTableHeader<WeatherForecast>> _headers = new List<DataTableHeader<WeatherForecast>> {
        new (){ Text = "Date",Value = nameof(WeatherForecast.Date) },
        new (){ Text = "Temp. (C)",Value = nameof(WeatherForecast.TemperatureC) },
        new (){ Text = "Temp. (F)",Value = nameof(WeatherForecast.TemperatureF) },
        new (){ Text = "Summary",Value = nameof(WeatherForecast.Summary),Sortable = false}
    };

    private WeatherForecast[]? forecasts;

    protected override async Task OnInitializedAsync()
    {
        forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
    }
}
